package io.gbznt.modules.buss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gbznt.common.utils.Constant;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.sys.service.SysDeptService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;

import io.gbznt.modules.buss.dao.BussRtuInfoDao;
import io.gbznt.modules.buss.entity.BussRtuInfoEntity;
import io.gbznt.modules.buss.service.BussRtuInfoService;


@Service("bussRtuInfoService")
public class BussRtuInfoServiceImpl extends ServiceImpl<BussRtuInfoDao, BussRtuInfoEntity> implements BussRtuInfoService {

    @Autowired
    private BussFarmlandInfoService bussFarmlandInfoService;
    @Autowired
    private SysDeptService sysDeptService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String serialNum = (String)params.get("serialNum");
        //田块id
        String farmlandId=(String)params.get("farmlandId");

        IPage<BussRtuInfoEntity> page = this.page(
                new Query<BussRtuInfoEntity>().getPage(params),
                new QueryWrapper<BussRtuInfoEntity>()
                    .like(StringUtils.isNotEmpty(serialNum),"serial_num",serialNum)
                    .eq(StringUtils.isNotEmpty(farmlandId),"farmland_info_id", farmlandId)
                    // 个人数据权限控制
                    .in(!Constant.SUPER_ADMIN.equals(ShiroUtils.getUserId()),
                            "dept_id", sysDeptService.getPermissionDeptIdsArr(true,true))
                    .orderByDesc("create_time")
        );

        for (BussRtuInfoEntity bussRtuInfoEntity: page.getRecords()) {
            // 填充田块名称
            BussFarmlandInfoEntity bussFarmlandInfoEntity = bussFarmlandInfoService.getById(
                    bussRtuInfoEntity.getFarmlandInfoId());
            if(null != bussFarmlandInfoEntity) {
                bussRtuInfoEntity.setFarmlandName(bussFarmlandInfoEntity.getName());
            }

        }

        return new PageUtils(page);
    }

}
